package 死锁;

import java.util.concurrent.locks.ReentrantLock;

public class _02_死锁 {

    public static void main(String[] args) {
        ReentrantLock lock1 = new ReentrantLock();
        ReentrantLock lock2 = new ReentrantLock();

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock1.lock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("线程1拿到锁1了");
                lock2.lock();
                System.out.println("线程1拿到锁2了");
                lock2.unlock();
                lock1.unlock();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock2.lock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("线程2拿到锁2了");
                lock1.lock();
                System.out.println("线程2拿到锁1了");
                lock1.unlock();
                lock2.unlock();
            }
        }).start();

    }
}
